home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / WINSOCK.PAK / DLGADDR.CPP next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  134 lines

  1. /*-----------------------------------------------------------------------*\
  2. | OWLSock Demo For Windows v1.0                                           |
  3. --------------------------------------------------------------------------|
  4. | Written By:  Paul Pedriana                                              |
  5. | Date:        May 7, 1995.                                               |
  6. | Copyright:   Copyright (c) 1995 by Paul Pedriana.  All Rights Reserved. |
  7. | UserID(s):   70541,3223                                                 |
  8. |              70541.3223@compuserve.com                                  |
  9. --------------------------------------------------------------------------|
  10. | This OWLSock demo is an application that demonstrates some features     |
  11. | of OWLSock.  It uses only asynchronous (non-blocking) Winsock calls,    |
  12. | and uses OWLSock socket 'external' notification rather than internal    |
  13. | notification.  External notification is the way most Winsock apps do    |
  14. | FD_XXX notifications; see the OWLSock docs for more info.               |
  15. --------------------------------------------------------------------------|
  16. | Notes on this module:                                                   |
  17. |    This module is a dialog box that implements the host entry (hostent) |
  18. | getXbyY functions.  It simply accepts an address in DNS format or       |
  19. | dotted-decimal format and converts it to an IP address and shows you    |
  20. | that address in dotted-decimal format.  Every TCP/IP program needs to   |
  21. | be able to do this conversion.                                          | 
  22. \*-----------------------------------------------------------------------*/
  23.  
  24. #include <owl/pch.h>
  25. #if !defined(OWL_DIALOG_H)
  26. # include <owl/dialog.h>
  27. #endif
  28. #if !defined(OWL_WINSOCK_H)
  29. # include <owl/winsock.h>
  30. #endif
  31. #include "dlgaddr.h"
  32.  
  33. //
  34. //
  35. //
  36. DEFINE_RESPONSE_TABLE1(DlgConvertAddress, TDialog)
  37.   EV_CHILD_NOTIFY(IDC_BTN_CONVERT, BN_CLICKED, CmBtnConvert),
  38.   EV_MESSAGE(MSG_HOST_INFO_NOTIFY, DoNotification),
  39. END_RESPONSE_TABLE;
  40.  
  41. DlgConvertAddress::DlgConvertAddress(TWindow *parent, TResId resId, TModule *module)
  42. :
  43.   TDialog(parent, resId, module), myPresentState(nIdle)
  44. {
  45. }
  46.  
  47.  
  48.  
  49. void DlgConvertAddress::SetupWindow()
  50. {
  51.   editNameCtrl    = new TEdit(this, IDC_EDIT_NAME,      255);
  52.   editAddressCtrl = new TEdit(this, IDC_EDIT_ADDRESS,    20);
  53.   staticStatus    = new TStatic(this, IDC_STATIC_STATUS, 32);
  54.   btnConvert      = new TButton(this, IDC_BTN_CONVERT);
  55.  
  56.   TDialog::SetupWindow();
  57. }
  58.  
  59.  
  60. LRESULT DlgConvertAddress::DoNotification(WPARAM, LPARAM lParam)
  61. {
  62.   char szAddressOutput[256];
  63.   int  nError = (int)WSAGETASYNCERROR(lParam);
  64.  
  65.   ::MessageBeep(10);
  66.   if (nError) {
  67.     MessageBox(TSocketError(nError).AppendError("Error looking up host address."), "Error", MB_OK);
  68.   }
  69.   else {
  70.     myHostInfoManager.HostEntryToAddress(myHostInfoManager.HostEntry, szAddressOutput);
  71.     editAddressCtrl->SetWindowText(szAddressOutput);
  72.   }
  73.   GoToIdleState();
  74.   return 1;
  75. }
  76.  
  77.  
  78. void DlgConvertAddress::CmBtnConvert()
  79. {
  80.   short  bAddressIsDottedDecimal;
  81.   char   szAddressInput[256];
  82.   int    nError;
  83.   HANDLE hHostRequest;
  84.  
  85.   if (myPresentState == nWaitingForAddress){ //If user wanted to cancel the operation...
  86.     myHostInfoManager.CancelHostRequest(); //Cancels the most recent request made by this HostInfoManager
  87.     GoToIdleState();
  88.     return;
  89.   }
  90.   GoToWaitingForAddressState();
  91.   editAddressCtrl->Clear(); //Clear the text.
  92.   editNameCtrl->GetWindowText(szAddressInput, 255);
  93.   bAddressIsDottedDecimal = TINetSocketAddress::IsAddressDottedDecimal(szAddressInput);
  94.   if(bAddressIsDottedDecimal){
  95.     editAddressCtrl->SetWindowText(szAddressInput);  //User intered the address directly.
  96.     GoToIdleState();
  97.   }
  98.   else{
  99.     nError = myHostInfoManager.GetHostInfoAsync(*this, hHostRequest, szAddressInput);
  100.     if(nError == WINSOCK_ERROR){
  101.       MessageBox(TSocketError(myHostInfoManager.GetLastError()).GetReasonString(), "Error", MB_OK);
  102.       GoToIdleState();
  103.     }
  104.   }
  105. }
  106.  
  107.  
  108. void DlgConvertAddress::CmOk()
  109. {
  110.   if(myPresentState != nIdle){
  111.     CmBtnConvert();  //Causes a cancellation of the request.
  112.   }
  113.   TDialog::CmOk(); //We are done and can close this dialog.
  114. }
  115.  
  116.  
  117. void DlgConvertAddress::GoToIdleState()
  118. {
  119.   btnConvert->SetWindowText("--> Convert -->");
  120.   staticStatus->SetWindowText("Status: Idle");
  121.   myPresentState = nIdle;
  122. }
  123.  
  124.  
  125. void DlgConvertAddress::GoToWaitingForAddressState()
  126. {
  127.   btnConvert->SetWindowText("Cancel");
  128.   staticStatus->SetWindowText("Status: Waiting for address...");
  129.   myPresentState = nWaitingForAddress;
  130. }
  131.  
  132.  
  133.  
  134.